package com.furentech.iotserver.fa.sinopec.sinopec_processor;

import com.furentech.iotserver.fa.server.ChannelUtil;
import com.furentech.iotserver.fa.sinopec.SinopecFunCode;
import com.furentech.iotserver.fa.sinopec.SinopecRestRequest;
import com.furentech.iotserver.fa.utils.BCD8421Operater;
import io.netty.channel.Channel;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;




@Component
public class Poll_Processor extends SinopecProcessorBase {
    @Override
    String actualGetJsonString(byte[] buf) {
        return null;
    }

    @Override
    String actualGetReadableString(byte[] buf) {
        return "查询版本指令";
    }

    @Override
    byte[] actualGetResponse(byte[] buf, Channel channel) {

//        //1B
//        if (ChannelUtil.getCbRecordFlag(ChannelUtil.currentChannel)) {
//
//        }
//        //22
//        else if (ChannelUtil.getReqVTotFlag(ChannelUtil.currentChannel)) {
//
//        }
//        //28
//        else if (ChannelUtil.getReqDispenserInfoFlag(ChannelUtil.currentChannel)) {
//
//        }


        if(ChannelUtil.getAuthReq2Flag(channel))
        {
            ChannelUtil.setAuthReq2Flag(channel, false);

            Byte tempBytes[];
            tempBytes=ChannelUtil.getAuthReq2RespBuf(channel);
            byte[] responseBuf = new byte[tempBytes.length];

            for (int i = 0; i < tempBytes.length; i++) {
                responseBuf[i] = tempBytes[i];
            }

            return responseBuf;
        }
        else if(ChannelUtil.getReqSignInFlag(channel))
        {
            ChannelUtil.setReqSignInFlag(channel, false);

            Byte tempBytes[];
            tempBytes=ChannelUtil.getReqSignInRespBuf(channel);
            byte[] responseBuf = new byte[tempBytes.length];

            for (int i = 0; i < tempBytes.length; i++) {
                responseBuf[i] = tempBytes[i];
            }

            return responseBuf;
        }
        else if(ChannelUtil.getReqOnlineFlag(channel))
        {
            ChannelUtil.setReqOnlineFlag(channel, false);

            Byte tempBytes[];
            tempBytes=ChannelUtil.getReqOnlineRespBuf(channel);
            byte[] responseBuf = new byte[tempBytes.length];

            for (int i = 0; i < tempBytes.length; i++) {
                responseBuf[i] = tempBytes[i];
            }

            return responseBuf;
        }
        else if(ChannelUtil.getReqDebitFlag(channel))
        {
            ChannelUtil.setReqDebitFlag(channel, false);

            Byte tempBytes[];
            tempBytes=ChannelUtil.getReqDebitRespBuf(channel);
            byte[] responseBuf = new byte[tempBytes.length];

            for (int i = 0; i < tempBytes.length; i++) {
                responseBuf[i] = tempBytes[i];
            }

            return responseBuf;
        }
        else if(ChannelUtil.getReqReverseFlag(channel))
        {
            ChannelUtil.setReqReverseFlag(channel, false);

            Byte tempBytes[];
            tempBytes=ChannelUtil.getReqReverseRespBuf(channel);
            byte[] responseBuf = new byte[tempBytes.length];

            for (int i = 0; i < tempBytes.length; i++) {
                responseBuf[i] = tempBytes[i];
            }

            return responseBuf;
        }
        else {

            String dateString = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            byte[] dateBcdBuf = BCD8421Operater.string2Bcd(dateString);

            byte[] responseBuf = new byte[17];
            responseBuf[0] = SinopecFunCode.Ver_Info;
            System.arraycopy(dateBcdBuf, 0, responseBuf, 1, 7);

            VerInfo verInfo = SinopecRestRequest.getVerInfo();

            if (verInfo == null) {
                return null;
            }

            responseBuf[8] = (byte) (0x00ff & (verInfo.getBL_VER() / 256));
            responseBuf[9] = (byte) (0x00ff & (verInfo.getBL_VER() % 256));
            responseBuf[10] = (byte) (0x00ff & (verInfo.getADD_BL_VER() % 256));
            responseBuf[11] = (byte) (0x00ff & (verInfo.getDEL_BL_VER() % 256));
            responseBuf[12] = (byte) (0x00ff & (verInfo.getWH_VER() % 256));
            responseBuf[13] = (byte) (0x00ff & (verInfo.getPRC_VER() % 256));
            responseBuf[14] = (byte) (0x00ff & (verInfo.getSta_VER() % 256));

            responseBuf[15] = 0;
            responseBuf[16] = 0;

            return responseBuf;
        }
    }
}

